package com.threeonemeeting.activity;

import java.util.ArrayList;
import java.util.Map;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;

import com.snda.lib.http.TaskManager;
import com.threeonemeeting.Const;
import com.threeonemeeting.DataCenter;
import com.threeonemeeting.R;
import com.threeonemeeting.activity.ext.PageListActivity;
import com.threeonemeeting.adapter.MessageInfoListAdapter;
import com.threeonemeeting.listview.DragRefreshListView;
import com.threeonemeeting.model.MessageInfo;
import com.threeonemeeting.tab.MultiActivityTab;
import com.threeonemeeting.task.GetMessageListTask;
import com.threeonemeeting.util.MiscUtil;

public class MainMessageActivity extends PageListActivity implements
		OnClickListener {

	public static final int TAB_NONE = -1;
	public static final int TAB_INBOX = 0;
	public static final int TAB_OUTBOX = 1;
	public static final int TAB_SYSTEMMSG = 2;

	private Integer mCurrentTabIndex = TAB_NONE;
	private Button mBtnInBox = null;
	private Button mBtnOutBox = null;
	private Button mBtnSystemMsg = null;

	private MessageInfoListAdapter mAdapter;

	protected boolean bForceRefresh = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_message, "");

		bForceRefresh = getIntent().getExtras().getBoolean("refresh");
		mCurrentTabIndex = getIntent().getExtras().getInt("type");
		if (mCurrentTabIndex == null)
			mCurrentTabIndex = TAB_NONE;

		mBtnInBox = (Button) findViewById(R.id.btn_inbox);
		mBtnOutBox = (Button) findViewById(R.id.btn_outbox);
		mBtnSystemMsg = (Button) findViewById(R.id.btn_system);
		mBtnInBox.setOnClickListener(this);
		mBtnOutBox.setOnClickListener(this);
		mBtnSystemMsg.setOnClickListener(this);

		// ListView
		mAdapter = new MessageInfoListAdapter(this);
		meetingListView.setAdapter(mAdapter);
		meetingListView
				.setOnRefreshListener(new DragRefreshListView.OnRefreshListener() {
					public void onRefresh() {
						getNextPage(1, true);
					}
				});

		meetingListView
				.setOnLoadMoreListener(new DragRefreshListView.OnLoadMoreListener() {
					public void onLoadMore(int nNextPage) {
						getNextPage(nNextPage);
						showLoadingMoreLayout(true);
					}
				});

		if (mCurrentTabIndex == TAB_NONE) {
			setCurrentTab(TAB_INBOX);
		} else {
			setCurrentTab(mCurrentTabIndex);
		}

		ImageButton mBtnSendMessage = (ImageButton) findViewById(R.id.btn_sendmessage);
		if (mBtnSendMessage != null) {
			mBtnSendMessage.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					// 跳转到会议详情页面
					Bundle b = new Bundle();
					b.putInt("type", mCurrentTabIndex);
					startActivity("SendMessageActivity", b);
				}
			});
		}

		// 发送请求，请求消息列表
		getNextPage(1, bForceRefresh);
	}

	protected void setCurrentTab(int tabIndex) {
		setTabButtonStatus(mCurrentTabIndex, false);
		setTabButtonStatus(tabIndex, true);
		mCurrentTabIndex = tabIndex;

		meetingListView.reset();
	}

	private void setTabButtonStatus(int tabIndex, boolean focus) {

		switch (tabIndex) {
		case TAB_INBOX:
			if (focus) {
				mBtnInBox.setTextAppearance(this,
						android.R.style.TextAppearance_Theme);
				mBtnInBox.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.btn_style_red_left_round_selected));
			} else {
				mBtnInBox.setTextAppearance(this,
						android.R.style.TextAppearance_Theme);
				mBtnInBox.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.btn_style_red_left_round_default));
			}
			break;

		case TAB_OUTBOX:
			if (focus) {
				mBtnOutBox.setTextAppearance(this,
						android.R.style.TextAppearance_Theme);
				mBtnOutBox.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.btn_style_red_no_round_selected));
			} else {
				mBtnOutBox.setTextAppearance(this,
						android.R.style.TextAppearance_Theme);
				mBtnOutBox.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.btn_style_red_no_round_default));
			}
			break;

		case TAB_SYSTEMMSG:
			if (focus) {
				mBtnSystemMsg.setTextAppearance(this,
						android.R.style.TextAppearance_Theme);
				mBtnSystemMsg.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.btn_style_red_right_round_selected));
			} else {
				mBtnSystemMsg.setTextAppearance(this,
						android.R.style.TextAppearance_Theme);
				mBtnSystemMsg.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.btn_style_red_right_round_default));
			}
			break;

		default:
			break;
		}
	}

	/**
	 * @desc 请求返回下一页的列表内容 子类重载该函数
	 * @param nPage
	 */
	protected void getNextPage(int nPage) {
		getNextPage(nPage, false);
	}

	/**
	 * @desc 请求返回下一页的列表内容 子类重载该函数
	 * @param nPage
	 */
	protected void getNextPage(int nPage, boolean bRefresh) {

		// 发送请求，请求消息列表
		GetMessageListTask task = new GetMessageListTask(this, this);
		if (bRefresh == true) {
			task.setRefresh(true);
		}

		task.setPages(nPage);
		Bundle params = new Bundle();
		params.putString("type", String.valueOf(mCurrentTabIndex));
		params.putString(
				"uid",
				String.valueOf(DataCenter.getInstance().currentLoginUserInfo.nUserId));
		params.putString("pass",
				DataCenter.getInstance().currentLoginUserInfo.strPassword);
		params.putString("page", String.valueOf(nPage));
		TaskManager.addTask(task, params, true);
	}

	public void onClick(View v) {

		if (v == mBtnInBox) {
			setCurrentTab(TAB_INBOX);
		} else if (v == mBtnOutBox) {
			setCurrentTab(TAB_OUTBOX);
		} else if (v == mBtnSystemMsg) {
			setCurrentTab(TAB_SYSTEMMSG);
		}

		meetingListView.reset();

		getNextPage(1);
		MiscUtil.showLoadingLayout(this);
	}

	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		int nIndex = arg2 - 1;
		if (nIndex < 0)
			return;

		if (nIndex > mAdapter.getCount() - 1)
			return;

		MessageInfo message = mAdapter.getItem(nIndex);
		if (message == null)
			return;

		// 跳转到消息详情页面
		Bundle b = new Bundle();
		b.putSerializable("message", message);
		b.putInt("type", mCurrentTabIndex);

		((MultiActivityTab) MainMessageActivity.this.getParent())
				.setCurrentTab("MessageDetailListActivity", b);
	}

	@SuppressWarnings("unchecked")
	public void onSuccessCallBack(Map<String, Object> mapData) {
		meetingListView.stopLoadingMore();

		ArrayList<MessageInfo> listMessage = (ArrayList<MessageInfo>) mapData
				.get("list");
		if (listMessage == null)
			return;

		Integer nPage = (Integer) mapData.get("page");
		if (nPage == null)
			nPage = 1;

		if (nPage == 1) {
			mAdapter.clear();
		}

		if (listMessage.size() == 0 || listMessage.size() < Const.PAGE_NUM) {
			meetingListView.setLastPage(nPage);
		}

		for (int i = 0; i < listMessage.size(); i++) {
			MessageInfo info = listMessage.get(i);
			if (info == null)
				continue;

			if (mCurrentTabIndex == 2) {
				info.strFromName = "系统管理员";
			}

			mAdapter.add(info);
		}

		showListLayout(listMessage.size());
		showLoadingMoreLayout(false);

		mAdapter.notifyDataSetChanged();
		meetingListView.onRefreshComplete();
		meetingListView.onLoadMoreComplete();
	}
}
